package com.lubase.wfengine.service.filter;

import com.lubase.core.service.UserInfoService;
import com.lubase.orm.model.LoginUser;
import com.lubase.orm.service.AppHolderService;
import com.lubase.orm.util.SpringUtil;
import lombok.SneakyThrows;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.apache.tomcat.websocket.AuthenticationException;

@Activate(group = "provider")
public class ProviderFilter implements Filter {
    @SneakyThrows
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String token = RpcContext.getServerAttachment().getAttachment("lubase-token");
        LoginUser user = SpringUtil.getApplicationContext().getBean(UserInfoService.class).verifyToken(token);
        if (user == null) {
            throw new AuthenticationException("用户信息已经失效");
        }
        SpringUtil.getApplicationContext().getBean(AppHolderService.class).setUser(user);
        Result result = invoker.invoke(invocation);
        return result;
    }
}
